var a = 6378245.0
var ee = 0.00669342162296594323

export function gcj02towgs84(lon, lat) {
  if (out_of_china(lon, lat)) {
    return [lon, lat]
  } else {
    var dlat = transformlat(lon - 105.0, lat - 35.0);
    var dlon = transformlng(lon - 105.0, lat - 35.0);
    var radlat = lat / 180.0 * Math.PI;
    var magic = Math.sin(radlat);
    magic = 1 - ee * magic * magic;
    var sqrtmagic = Math.sqrt(magic);
    dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * Math.PI);
    dlon = (dlon * 180.0) / (a / sqrtmagic * Math.cos(radlat) * Math.PI);
    var mglon = lon + dlon;
    var mlat = lat + dlat;
    return [lon * 2 - mglon, lat * 2 - mlat];
  }
}

export function transformlat(lon, lat) {
  var ret = -100.0 +
    2.0 * lon +
    3.0 * lat +
    0.2 * lat * lat +
    0.1 * lon * lat +
    0.2 * Math.sqrt(Math.abs(lon));
  ret += (20.0 * Math.sin(6.0 * lon * Math.PI) + 20.0 * Math.sin(2.0 * lon * Math.PI)) * 2.0 / 3.0;
  ret += (20.0 * Math.sin(lat * Math.PI) + 40.0 * Math.sin(lat / 3.0 * Math.PI)) * 2.0 / 3.0;
  ret += (160.0 * Math.sin(lat / 12.0 * Math.PI) + 320 * Math.sin(lat * Math.PI / 30.0)) * 2.0 / 3.0;
  return ret
}

export function transformlng(lon, lat) {
  var ret = 300.0 + lon +
    2.0 * lat +
    0.1 * lon * lon +
    0.1 * lon * lat +
    0.1 * Math.sqrt(Math.abs(lon));
  ret += (20.0 * Math.sin(6.0 * lon * Math.PI) +
    20.0 * Math.sin(2.0 * lon * Math.PI)) * 2.0 / 3.0;
  ret += (20.0 * Math.sin(lon * Math.PI) + 40.0 *
    Math.sin(lon / 3.0 * Math.PI)) * 2.0 / 3.0;
  ret += (150.0 * Math.sin(lon / 12.0 * Math.PI) + 300.0 *
    Math.sin(lon / 30.0 * Math.PI)) * 2.0 / 3.0;
  return ret
}

export function out_of_china(lon, lat) {
  return (lon < 72.004 || lon > 137.8347 || lat < 0.8293 || lat > 55.8271);
}

export function BD09toGCJ02(lon, lat) {
  var X_PI = Math.PI * 3000.0 / 180.0
  var x = lon - 0.0065
  var y = lat - 0.006
  var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * X_PI)
  var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * X_PI)
  var gg_lng = z * Math.cos(theta)
  var gg_lat = z * Math.sin(theta)
  return [gg_lng, gg_lat]
}

export function BD09toWGS84(coordinate) {
  let GCJ02Coordinate = BD09toGCJ02(coordinate[0], coordinate[1])
  let WGS84Coordinate = gcj02towgs84(GCJ02Coordinate[0], GCJ02Coordinate[1])
  return WGS84Coordinate
}

export function wgs84ToGcj02ToBd09Public(lng, lat, ...args) {
  const xPI = (3.14159265358979324 * 3000.0) / 180.0;
  const PI = 3.1415926535897932384626;
  const a = 6378245.0;
  const ee = 0.00669342162296594323;
  // WGS84转GCj02
  let dlat = transformlat(lng - 105.0, lat - 35.0);
  let dlng = transformlng(lng - 105.0, lat - 35.0);
  let radlat = (lat / 180.0) * PI;
  let magic = Math.sin(radlat);
  magic = 1 - ee * magic * magic;
  let sqrtmagic = Math.sqrt(magic);
  dlat = (dlat * 180.0) / (((a * (1 - ee)) / (magic * sqrtmagic)) * PI);
  dlng = (dlng * 180.0) / ((a / sqrtmagic) * Math.cos(radlat) * PI);
  let mglat = lat + dlat;
  let mglng = lng + dlng;
  // 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换
  let z =
    Math.sqrt(mglng * mglng + mglat * mglat) + 0.00002 * Math.sin(mglat * xPI);
  let theta = Math.atan2(mglat, mglng) + 0.000003 * Math.cos(mglng * xPI);
  let bdlng = z * Math.cos(theta) + 0.0065;
  let bdlat = z * Math.sin(theta) + 0.006;
  return [bdlng, bdlat]
  /*  Object.assign({}, target, source)，其中第一个参数是一个空对象，第二个参数是目标对象，第三个参数是源对象。该方法会将源对象中不存在于目标对象中的属性复制到目标对象中，而不会覆盖目标对象中已经存在的同名属性。 */
  // return Object.assign({}, {longitude: bdlng, latitude: bdlat}, ...args);
}

export function WGS84toBD09(coordinate) {
  return wgs84ToGcj02ToBd09Public(coordinate[0], coordinate[1])
}
